const axios = require("axios");
const fs = require("fs");
const path = require("path");
const envPath = path.resolve(__dirname, "..", ".env.development");
const envText = fs.readFileSync(envPath, "utf8");
const SERVERURL = /VUE_APP_SERVERURL='(.+)'\r/g.exec(envText)[1];
const PASSWORD = /VUE_APP_PASSWORD='(.+)'\r/g.exec(envText)[1];
const rootPath = path.resolve(__dirname, "..", "src", "pages");
const loginParams = {
  username: "admin",
  password: PASSWORD
};
login().then(res => {
  const token = `${res.data.token_type} ${res.data.access_token}`;
  loginParams.scope = res.data.scope;
  getMenuTree(token).then(menu => {
    createRouter(menu.data.items);
  });
});
function login() {
  return new Promise((resolve, reject) => {
    axios
      .post(SERVERURL + "/api/account/token", loginParams)
      .then(res => {
        resolve(res);
      })
      .catch(() => {
        console.log("登录失败！");
        reject();
      });
  });
}
function getMenuTree(token) {
  return new Promise((resolve, reject) => {
    axios
      .get(`${SERVERURL}/api/${loginParams.scope}/sys-menu/trees`, {
        headers: { Authorization: token }
      })
      .then(res => {
        resolve(res);
      })
      .catch(() => {
        console.log("获取菜单目录失败！");
        reject();
      });
  });
}
function createRouter(datas) {
  datas.forEach(menu => {
    buildFile(menu.component, menu.name);
    createChildren(menu);
  });
}
function createChildren(menu) {
  if (menu.children) {
    menu.children.forEach(submenu => {
      buildFile(submenu.component, submenu.name);
      createChildren(submenu);
    });
  }
  if (menu.submenusComponent && menu.submenusComponent.length) {
    createSubMenus(menu.submenusComponent, menu.component, menu.name);
  }
  return null;
}
function createSubMenus(subMenus, path, title) {
  buildFile(path, title);
  createComponentsDeep(subMenus);
}
function createComponentsDeep(components) {
  components.forEach(menu => {
    buildFile(menu.path, menu.title);
    if (menu.children && menu.children.length) {
      createComponentsDeep(menu.children);
    }
  });
}
function buildFile(dirpath, title) {
  const relativePath = ["index", "views", ...dirpath.split("/")];
  const filePath = path.resolve(rootPath, ...relativePath);
  if (!fs.existsSync(filePath)) {
    createDirs(rootPath, relativePath);
  }
  if (!fs.existsSync(path.resolve(filePath, "Index.vue"))) {
    const temp = getVueTemplate(title);
    fs.writeFile(path.resolve(filePath, "Index.vue"), temp, "utf8", function() {
      console.log(path.resolve(filePath, "Index.vue") + " 生成成功！");
    });
  }
}
function getVueTemplate(title) {
  return `<!-- ${title || ""} -->
<template>
  <div></div>
</template>

<script>
export default {
  components: {},
  data() {
    return {};
  },
  methods: {},
  mounted() {}
};
</script>

<style lang="scss" scoped></style>
`;
}
function createDirs(rootPath, relativePath) {
  if (relativePath.length) {
    const curDir = relativePath.shift();
    const curPath = path.resolve(rootPath, curDir);
    const isExit = fs.existsSync(curPath);
    if (isExit) {
      createDirs(curPath, relativePath);
    } else {
      fs.mkdirSync(curPath);
      createDirs(curPath, relativePath);
    }
  }
}
